package com.shareinfo.adminManage.auto.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.WebUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;

import com.shareinfo.base.pagination.PageInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import com.shareinfo.adminManage.auto.model.AutoDbTableFieldModel;
import com.shareinfo.adminManage.auto.query.AutoDbTableFieldQuery;
import com.shareinfo.adminManage.auto.service.AutoDbTableFieldService;

@Controller
@Scope("prototype")
@RequestMapping(value = "/adminManage/sys/autoDbTableField")
public class AutoDbTableFieldController {
	
  @Resource(name = "autoDbTableFieldService")
  private AutoDbTableFieldService autoDbTableFieldService;
  
  @ModelAttribute
	public AutoDbTableFieldModel get(@RequestParam(required=false) java.lang.String id) {
		if (id!=null){
			return autoDbTableFieldService.getById(id);
		}else{
			return new AutoDbTableFieldModel();
		}
	}
  
  	/** 
	 * 执行搜索 
	**/
	@RequestMapping(value = {"list"})
	public ModelAndView list(AutoDbTableFieldQuery autoDbTableFieldQuery,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<AutoDbTableFieldModel> pageInfo = autoDbTableFieldService.selectList(autoDbTableFieldQuery);
		
		ModelAndView result = new ModelAndView("autoDbTableFieldList");
		result.addObject("pageInfo", pageInfo);
		return result;
	}
	
	@RequestMapping(value = {"listForEasyui"})
	@ResponseBody
	public Map<String, Object> listForEasyui(AutoDbTableFieldQuery autoDbTableFieldQuery,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<AutoDbTableFieldModel> pageInfo = autoDbTableFieldService.selectList(autoDbTableFieldQuery);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(PageInfo.EASYUI_ROWS, pageInfo.getList());
		map.put(PageInfo.EASYUI_TOTAL, pageInfo.getTotalRecord());
		return map;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = {"show"})
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbTableFieldModel autoDbTableField = (AutoDbTableFieldModel)autoDbTableFieldService.getById(id);
		return new ModelAndView("autoDbTableFieldForm","autoDbTableField",autoDbTableField);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = {"create"})
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,AutoDbTableFieldModel autoDbTableFieldModel) throws Exception {
		return new ModelAndView("autoDbTableFieldForm","autoDbTableField",autoDbTableFieldModel);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = {"save"})
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,AutoDbTableFieldModel autoDbTableFieldModel) throws Exception {
		autoDbTableFieldService.save(autoDbTableFieldModel);
		return new ModelAndView("autoDbTableFieldList");
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = {"edit"})
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		AutoDbTableFieldModel autoDbTableFieldModel = (AutoDbTableFieldModel)autoDbTableFieldService.getById(id);
		return new ModelAndView("autoDbTableFieldForm","autoDbTableField",autoDbTableFieldModel);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = {"update"})
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.String id = new java.lang.String(request.getParameter("id"));
		
		AutoDbTableFieldModel autoDbTableFieldModel = (AutoDbTableFieldModel)autoDbTableFieldService.getById(id);
		Map params = WebUtils.getParametersStartingWith(request, "");
		BeanUtilsBean beanUtils = new BeanUtilsBean();
		beanUtils.copyProperties(autoDbTableFieldModel, params);
		autoDbTableFieldService.update(autoDbTableFieldModel);
		return new ModelAndView("autoDbTableFieldList");
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = {"delete"})
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		for(int i = 0; i < ids.length; i++) {
			autoDbTableFieldService.deleteById(ids[i]);
		}
		return new ModelAndView("autoDbTableFieldList");
	}

}

